package com.rabbiter.association.controller;

import com.rabbiter.association.entity.Members;
import com.rabbiter.association.entity.Users;
import com.rabbiter.association.handle.CacheHandle;
import com.rabbiter.association.msg.PageData;
import com.rabbiter.association.msg.R;
import com.rabbiter.association.service.MembersService;
import com.rabbiter.association.service.UsersService;
import com.rabbiter.association.utils.IDUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 系统请求响应控制器
 * 成员信息
 */
@Controller
@RequestMapping("/members")
@Api(tags = "成员管理")
@RequiredArgsConstructor
public class MembersController extends BaseController {

    protected static final Logger Log = LoggerFactory.getLogger(MembersController.class);


    private final CacheHandle cacheHandle;

    private final UsersService usersService;

    private final MembersService membersService;

    @RequestMapping("")
    @ApiOperation(value = "分页查询成员")
    public String index() {

        return "pages/Members";
    }

    @GetMapping("/info")
    @ResponseBody
    @ApiOperation(value = "通过ID查询成员信息")
    public R getInfo(String id) {

        Log.info("查找指定成员信息，ID：{}", id);

        Members members = membersService.getOne(id);

        return R.successData(members);
    }

    @GetMapping("/page")
    @ResponseBody
    @ApiOperation(value = "分页查找成员信息")
    public R getPageInfos(Long pageIndex, Long pageSize,
                          String token, String teamName, String userName) {

        Users user = usersService.getOne(cacheHandle.getUserInfoCache(token));
        if (ObjectUtils.isEmpty(user)) {
            return R.error("登录信息不存在，请重新登录");
        }
        if (user.getType() == 0) {
            Log.info("分页查找成员信息，当前页码：{}，" + "每页数据量：{}, 模糊查询，团队名称：{}，用户姓名：{}", pageIndex, pageSize, teamName, userName);
            PageData page = membersService.getPageAll(pageIndex, pageSize, teamName, userName);

            return R.successData(page);
        } else {

            Log.info("分页查找成员信息，当前页码：{}，"
                            + "每页数据量：{}, 模糊查询，团队名称：{}，用户姓名：{}", pageIndex,
                    pageSize, teamName, userName);

            PageData page = membersService.getPageByManId(pageIndex, pageSize, user.getId(), teamName, userName);

            return R.successData(page);
        }
    }


    @PostMapping("/add")
    @ResponseBody
    public R addInfo(@RequestBody Members members) {

        members.setId(IDUtils.makeIDByCurrent());

        Log.info("添加成员信息，传入参数：{}", members);

        membersService.add(members);

        return R.success();
    }

    @PostMapping("/upd")
    @ResponseBody
    public R updInfo(@RequestBody Members members) {

        Log.info("修改成员信息，传入参数：{}", members);

        membersService.update(members);

        return R.success();
    }

    @PostMapping("/del")
    @ResponseBody
    public R delInfo(@RequestBody Map<String, String> map) {

        final String id = map.get("id");
        Members members = membersService.getOne(id);

        if (membersService.isManager(members.getTeamId(), members.getUserId())) {

            return R.warn("社团管理员无法移除");
        } else {

            Log.info("删除成员信息, ID:{}", id);

            membersService.delete(members);

            return R.success();
        }
    }

    //    http://localhost:9999/api/members/getTeamUser?token=ee9c0123e004451b964722fabe48fef2
    @GetMapping("/getTeamUser")
    @ResponseBody
    public R getTeamUser(String token) {
        Users user = usersService.getOne(cacheHandle.getUserInfoCache(token));
        if (ObjectUtils.isEmpty(user)) {
            return R.error("登录信息不存在，请重新登录");
        }
        return R.successData(user);
    }

}